package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Y1n
 * @date 2021/11/19 14:06
 * @decs  根据inputstream把请求的 信息封装成request 对象
 **/
public class Request {

    private String method ;  // 请求方式 get post

    private String url;  // 例如 /   /index.html  /laogu

    private InputStream inputStream; // 其他属性 通过输入流解析

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 可能输入的是流因为网络的原因是间断的 所以 使用while去持续的去读 直到读到 退出循环
        int count = 0 ;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);

        // 根据换行符 获取请求头的 method 以及url
        String firstLine = inputStr.split("\\n")[0]; // GET / HTTP/1.1
        // 去根据 空格 获取method
        String[] firstLines = firstLine.split(" ");
        this.method = firstLines[0];
        this.url = firstLines[1];
        System.out.println("method = " + this.method);
        System.out.println("url = " + this.url);

    }
}
